package se.webbzon.boltzmann.game.object.player;

import java.awt.Color;

import se.webbzon.boltzmann.configuration.Graphics;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.collision3d.CollisionCube3D;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;
import se.webbzon.oschi01.worldobject.appearance.Appearance3D;
import se.webbzon.oschi01.worldobject.appearance.NoAppearance3D;
import se.webbzon.oschi01.worldobject.appearance.SimpleAppearance3D;
import se.webbzon.oschi01.worldobject.appearance.XZPlaneAppearance3D;

public class AbstractPlayerCharacterShadow extends WorldStaticObject {
	
	// Whatever this shadow is enabled or not
	private final boolean enabled;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a game player character shadow. The game player shadow 
	 * will be visible only if the game player is standing on a solid
	 * ground or if the game player is above the solid ground. The
	 * game shadow therefore needs a collection of all object to ignore
	 * when performing collision checking. The size of this collection
	 * must not be increased after initialization. **/
	public AbstractPlayerCharacterShadow() {
		super(WorldObject.onStep,createShadow());
		enabled = Graphics.instance().hasShadows();
		if (enabled) {
			setMask(new CollisionCube3D());
			this.setSize(16,5,8);
		}
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void onStep(WorldArea world) {
		if (enabled) {
			setSolid(true);
			if (world.placeFree(this, null, null, null))
				setVisible(false);
			else
				setVisible(true);
			setSolid(false);
		}
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Creates the shadow appearance **/
	private static Appearance3D createShadow() {
		if (Graphics.instance().hasShadows()) {
			SimpleAppearance3D appearance = new XZPlaneAppearance3D(1,
					SimpleAppearance3D.TRANSPARENCY +
					SimpleAppearance3D.LAYERS, false);

			appearance.setColor(Color.black);
			appearance.setTransparency(0.5f);
			appearance.setTextureSet(Resources.getTextureSet("effect_shadow_circle"));
			appearance.setLayer(0, 5, 0);

			return appearance;
		} else
			return new NoAppearance3D();
	}

}
